package com.may.nio;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.IntBuffer;
import java.util.stream.IntStream;

/**
 * @program: com.may.netty
 * @description:
 * @author: Bobby.Ma
 * @create: 2020-08-02 11:35
 **/
public class BasicBuffer {
    /**
     * logger
     */
    private static final Logger log = LoggerFactory.getLogger(BasicBuffer.class);

    /**
     * buffer 其实是个一个内存块，底层是一个数组
     */
    public static void main(String[] args) {
        //初始化 buffer 容量为 5
        IntBuffer buffer = IntBuffer.allocate(5);
        //向 buffer 中放值
        IntStream.range(0, buffer.capacity()).forEach(i -> buffer.put(i * 2));
        //读写切换
        buffer.flip();
        //当 buffer 中还有剩余的
        while (buffer.hasRemaining()) {
            //每取一次，指针后移
            log.info(String.valueOf(buffer.get()));
        }
    }
}
